package com.parenting.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.parenting.entities.CouponDO;
import com.parenting.entities.GiftDO;
import com.parenting.mapper.GiftMapper;
import com.parenting.model.IMPage;
import com.parenting.model.UserDTO;
import com.parenting.request.AddGiftRequest;
import com.parenting.request.SearchGiftPageListRequest;
import com.parenting.request.UpdateGiftRequest;
import com.parenting.response.GiftPageListResponse;
import com.parenting.response.GiftResponse;
import com.parenting.service.CouponService;
import com.parenting.service.GiftService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 连带赠表 服务实现类
 * </p>
 *
 * @author 烟台业达-王世玺
 * @since 2021-09-15
 */
@Service
@Slf4j
public class GiftServiceImpl extends ServiceImpl<GiftMapper, GiftDO> implements GiftService {
    @Autowired
    private CouponService  couponService;
    @Autowired
    private RedissonClient redissonClient;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveGift(AddGiftRequest request, UserDTO userDTO) {
        String lockKey = "lock:gift:"+request.getCouponId();
        RLock rLock = redissonClient.getLock(lockKey);
        rLock.lock(5, TimeUnit.SECONDS);
        try {
            GiftDO giftDO = BeanUtil.copyProperties(request, GiftDO.class);
            giftDO.setShopId(userDTO.getShopId());
            giftDO.setShopName(userDTO.getShopName());
            giftDO.setCreateDate(new Date());
            giftDO.setUpdateDate(new Date());
            baseMapper.insert(giftDO);
            couponService.updateNumberOfCouponCouponById(request.getGiftNumber(),request.getCouponId());
        }finally{
            rLock.unlock();
            log.info("解锁成功");
        }
    }

    @Override
    public IMPage<GiftPageListResponse> pageListGift(SearchGiftPageListRequest request) {
        IMPage<GiftPageListResponse> imPage=new IMPage<>();
        IPage<GiftPageListResponse> giftPageListResponseIPage = baseMapper.pageListGift(request.getPage(), request);
        imPage.setCurrent(giftPageListResponseIPage.getCurrent());
        imPage.setSize(giftPageListResponseIPage.getSize());
        imPage.setTotal(giftPageListResponseIPage.getTotal());
        imPage.setRecords(giftPageListResponseIPage.getRecords());
        return imPage;
    }

    @Override
    public GiftResponse getGiftResponseById(String id) {

        return baseMapper.getGiftResponseById(id);
    }

    @Override
    public void updateGift(UpdateGiftRequest request) {
        String lockKey = "lock:update:gift:"+request.getId();
        RLock rLock = redissonClient.getLock(lockKey);
        rLock.lock(5, TimeUnit.SECONDS);
     try {
         //判断加减
         GiftDO giftDO = baseMapper.selectById(request.getId());
         if (giftDO.getGiftNumber()>request.getGiftNumber()){
             //计算出 减了多少
             int i = request.getGiftNumber() - giftDO.getGiftNumber();
             //修改优惠券数据库库存
             couponService.updateNumberOfCouponCouponById(i,giftDO.getCouponId(),"add");
         }
         if (giftDO.getGiftNumber()<request.getGiftNumber()){
             //加数量
             int i = giftDO.getGiftNumber()-request.getGiftNumber();
             couponService.updateNumberOfCouponCouponById(i,giftDO.getCouponId(),"reduce");
         }
         giftDO.setGiftNumber(request.getGiftNumber());
         baseMapper.updateById(giftDO);
     }finally{
         rLock.unlock();
         log.info("解锁成功");
     }

    }
}
